#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n,m;
    string s1;
    string s2;

    vector<int> dp[2]={vector<int>(n + 1, 0),vector<int>(m + 1, 0)};
    int ans = 0;
    int k = 0;
    for (int i = 0; i < n; i++)
    {
        k = 1-k;
        for (int j = 0; j < m; j++)
        {
            if (s1[i] == s2[j])
                dp[k][j + 1] = dp[1-k][j] + 1;
            else
                dp[k][j + 1] = max(dp[1-k][j + 1], dp[k][j]);
        }
    }
    cout << dp[k][m];

    return 0;
}

int main2()
{
    int n,m;
    string s1;
    string s2;

    vector<vector<int>> dp(n + 1,vector<int>(m + 1, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (s1[i] == s2[j])
                dp[i+1][j + 1] = dp[i][j] + 1;
            else
                dp[j+1][j + 1] = max(dp[i][j + 1], dp[i+1][j]);
        }
    }
    cout << dp[n][m];

    return 0;
}
/*
7
432dd34

5
Ab3bd

*/